Completed
Push — master ( abe26e...97d0fb )
by Sander
9s
created

angular.controller(ꞌSettingsCtrlꞌ)   B

Complexity

Conditions 6
Paths 25

Size

Total Lines 287

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
nc 25
nop 12
dl 0
loc 287
rs 8.1463
c 2
b 1
f 0

How to fix   Long Method    Many Parameters   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
27
	/**
28
	 * @ngdoc function
29
	 * @name passmanApp.controller:SettingsCtrl
30
	 * @description
31
	 * # SettingsCtrl
32
	 * Controller of the passmanApp
33
	 */
34
	angular.module('passmanApp')
35
		.controller('SettingsCtrl', ['$scope', '$rootScope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$http', 'EncryptService', 'NotificationService', '$sce', '$translate',
36
			function ($scope, $rootScope, SettingsService, VaultService, CredentialService, $location, $routeParams, $http, EncryptService, NotificationService, $sce, $translate) {
37
				$scope.vault_settings = {};
38
				$scope.new_vault_name = '';
39
				$scope.active_vault = VaultService.getActiveVault();
40
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
41
					if (!$scope.active_vault) {
42
						$location.path('/');
43
						return;
44
					}
45
				} else {
46
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
47
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
48
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
49
						VaultService.setActiveVault(_vault);
50
						$scope.active_vault = _vault;
51
					}
52
				}
53
54
				VaultService.getVault($scope.active_vault).then(function (vault) {
55
					vault.vaultKey = VaultService.getActiveVault().vaultKey;
56
					delete vault.credentials;
57
					VaultService.setActiveVault(vault);
58
					$scope.vault_settings = vault.vault_settings;
59
					if (!$scope.vault_settings.hasOwnProperty('pwSettings')) {
60
						$scope.vault_settings.pwSettings = {
61
							'length': 12,
62
							'useUppercase': true,
63
							'useLowercase': true,
64
							'useDigits': true,
65
							'useSpecialChars': true,
66
							'minimumDigitCount': 3,
67
							'avoidAmbiguousCharacters': false,
68
							'requireEveryCharType': true,
69
							'generateOnCreate': true
70
						};
71
					}
72
				});
73
74
75
				var btn_txt = $translate.instant('bookmarklet.text');
76
				var http = location.protocol, slashes = http.concat("//"), host = slashes.concat(window.location.hostname), complete = host + location.pathname;
77
				$scope.bookmarklet = $sce.trustAsHtml("<a class=\"button\" href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,e=c(document.title),d=a.open('" + complete + "bookmarklet?url='+c(b.location)+'&title='+e,'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=750px,width=475px,resizable=0,alwaysRaised=1');a.setTimeout(function(){d.focus()},300);})();\">" + btn_txt + "</a>");
78
79
80
				$scope.saveVaultSettings = function () {
81
					var _vault = $scope.active_vault;
82
					_vault.name = $scope.new_vault_name;
83
					_vault.vault_settings = angular.copy($scope.vault_settings);
84
					VaultService.updateVault(_vault).then(function () {
85
						//VaultService.setActiveVault(_vault);
86
						$scope.active_vault.name = angular.copy(_vault.name);
87
						NotificationService.showNotification($translate.instant('settings.saved'), 5000);
88
					});
89
				};
90
91
92
				$scope.tabs = [
93
					{
94
						title: $translate.instant('settings.general'),
95
						url: 'views/partials/forms/settings/general_settings.html'
96
					},
97
					{
98
						title: $translate.instant('settings.audit'),
99
						url: 'views/partials/forms/settings/tool.html'
100
101
					},
102
					{
103
						title: $translate.instant('settings.password'),
104
						url: 'views/partials/forms/settings/password_settings.html'
105
106
					},
107
					{
108
						title: $translate.instant('settings.import'),
109
						url: 'views/partials/forms/settings/import.html'
110
111
					},
112
					{
113
						title: $translate.instant('settings.export'),
114
						url: 'views/partials/forms/settings/export.html'
115
116
					},
117
					{
118
						title: $translate.instant('settings.sharing'),
119
						url: 'views/partials/forms/settings/sharing.html'
120
					}
121
				];
122
123
				$scope.currentTab = $scope.tabs[0];
124
125
				$scope.onClickTab = function (tab) {
126
					$scope.currentTab = tab;
127
				};
128
129
				$scope.isActiveTab = function (tab) {
130
					return tab.url === $scope.currentTab.url;
131
				};
132
133
				var getPassmanVersion = function () {
134
					var url = OC.generateUrl('apps/passman/api/internal/version');
135
					$http.get(url).then(function (result) {
136
						$scope.passman_version = result.data.version;
137
					});
138
				};
139
				getPassmanVersion();
140
141
				$scope.$watch(function () {
142
					return VaultService.getActiveVault();
143
				}, function (vault) {
144
					if (vault) {
145
						$scope.active_vault = vault;
146
					}
147
				});
148
149
				$rootScope.$on('logout', function () {
150
					$scope.selectedVault = false;
151
				});
152
153
				var getCurrentVaultCredentials = function (callback) {
154
					VaultService.getVault($scope.active_vault).then(callback);
155
				};
156
157
				var decryptOwnCredentials = function (vault) {
158
					var _selected_credentials = [];
159
					if (vault.credentials.length === 0) {
160
						$location.path('/');
161
					}
162
					for (var i = 0; i < vault.credentials.length; i++) {
163
						var _credential = vault.credentials[i];
164
						var isShared = (_credential.shared_key === null || _credential.shared_key === '');
165
						if ( isShared || !_credential.hasOwnProperty('acl')) {
166
							var _success;
167
							try {
168
								CredentialService.decryptCredential(_credential, VaultService.getActiveVault().vaultKey);
169
								_success = true;
170
							} catch (e) {
171
								_success = false;
172
							}
173
							if (_success) {
174
								_selected_credentials.push(_credential);
175
							}
176
						}
177
					}
178
					return _selected_credentials;
179
				};
180
181
				$scope.startScan = function (minStrength) {
182
					getCurrentVaultCredentials(function (vault) {
183
						var results = [];
184
						for (var i = 0; i < vault.credentials.length; i++) {
185
							var c = angular.copy(vault.credentials[i]);
186
							if (c.password && c.hidden === 0) {
187
								try {
188
									c = CredentialService.decryptCredential(c);
189
									if (c.password) {
190
										var zxcvbn_result = zxcvbn(c.password);
191
										if (zxcvbn_result.score <= minStrength) {
192
											results.push({
193
												credential_id: c.credential_id,
194
												label: c.label,
195
												password: c.password,
196
												password_zxcvbn_result: zxcvbn_result
197
											});
198
										}
199
									}
200
								} catch (e) {
201
									console.warn(e);
202
								}
203
204
							}
205
							//@todo loop custom fields (if any and check secret fields
206
						}
207
						$scope.scan_result = results;
208
					});
209
				};
210
211
212
				$scope.cur_state = {};
213
214
215
				$scope.$on("$locationChangeStart", function (event) {
216
					if ($scope.change_pw) {
217
						if ($scope.change_pw.total > 0 && $scope.change_pw.done < $scope.change_pw.total) {
218
							if (!confirm($translate.instant('changepw.navigate.away.warning'))) {
219
								event.preventDefault();
220
							}
221
						}
222
					}
223
				});
224
225
226
				$scope.changeVaultPassword = function (oldVaultPass, newVaultPass, newVaultPass2) {
227
					if (oldVaultPass !== VaultService.getActiveVault().vaultKey) {
228
						$scope.error = $translate.instant('incorrect.password');
229
						return;
230
					}
231
					if (newVaultPass !== newVaultPass2) {
232
						$scope.error = $translate.instant('password.no.match');
233
						return;
234
					}
235
					SettingsService.setSetting('defaultVault', null);
236
					SettingsService.setSetting('defaultVaultPass', null);
237
					VaultService.getVault($scope.active_vault).then(function (vault) {
238
						var _selected_credentials = decryptOwnCredentials(vault);
239
						$scope.change_pw = {
240
							percent: 0,
241
							done: 0,
242
							total: _selected_credentials.length
243
						};
244
						var changeCredential = function (index, oldVaultPass, newVaultPass) {
245
							CredentialService.reencryptCredential(_selected_credentials[index].guid, oldVaultPass, newVaultPass).progress(function (data) {
246
								$scope.cur_state = data;
247
							}).then(function () {
248
								var percent = index / _selected_credentials.length * 100;
249
								$scope.change_pw = {
250
									percent: percent,
251
									done: index + 1,
252
									total: _selected_credentials.length
253
								};
254
								if (index < _selected_credentials.length - 1) {
255
									changeCredential(index + 1, oldVaultPass, newVaultPass);
256
								} else {
257
									vault.private_sharing_key = EncryptService.decryptString(angular.copy(vault.private_sharing_key), oldVaultPass);
258
									vault.private_sharing_key = EncryptService.encryptString(vault.private_sharing_key, newVaultPass);
259
									VaultService.updateSharingKeys(vault).then(function () {
260
										$rootScope.$broadcast('logout');
261
										NotificationService.showNotification($translate.instant('login.new.pass'), 5000);
262
									});
263
								}
264
							});
265
						};
266
						changeCredential(0, VaultService.getActiveVault().vaultKey, newVaultPass);
267
268
					});
269
				};
270
271
				$scope.confirm_vault_delete = false;
272
				$scope.delete_vault_password = '';
273
				$scope.delete_vault = function () {
274
					if ($scope.confirm_vault_delete && $scope.delete_vault_password === VaultService.getActiveVault().vaultKey) {
275
						getCurrentVaultCredentials(function (vault) {
276
							var credentials = vault.credentials;
277
							$scope.remove_pw = {
278
								percent: 0,
279
								done: 0,
280
								total: vault.credentials.length
281
							};
282
							var deleteCredential = function(index){
283
								$scope.translationData = {
284
									password:  credentials[index].label
285
								};
286
								CredentialService.destroyCredential(credentials[index].guid).then(function () {
287
									var percent = index / vault.credentials.length * 100;
288
									$scope.remove_pw = {
289
										percent: percent,
290
										done: index,
291
										total: vault.credentials.length
292
									};
293
									if(index === credentials.length-1){
294
										VaultService.deleteVault(vault).then(function () {
295
											SettingsService.setSetting('defaultVaultPass', false);
296
											SettingsService.setSetting('defaultVault', null);
297
											$rootScope.$broadcast('logout');
298
											$location.path('/');
299
										});
300
										return;
301
									}
302
									deleteCredential(index+1);
303
								});
304
							};
305
							deleteCredential(0);
306
						});
307
					}
308
309
				};
310
311
				$rootScope.$on('logout', function () {
312
					$scope.active_vault = null;
313
					VaultService.setActiveVault(null);
314
					$location.path('/');
315
316
				});
317
318
				$scope.cancel = function () {
319
					$location.path('/vault/' + $routeParams.vault_id);
320
				};
321
322
			}]);
323
324
}());